<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 组件-取消订单 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<view class="component-modal-cancel-order" @click.stop>
		<uni-popup ref="popupModal" type="bottom" :safe-area="false" @change="onChange">
			<view class="popup-box" :style="{'--theme-color': themeColor}">
				<view class="popup-header">
					<view class="title">确认取消订单</view>
					<view class="subtitle">请根据自身情况进行选择</view>
					<image class="close" src="/static/close.png" mode="aspectFit" @click="onClose"></image>
				</view>
				<view class="popup-content">
					<scroll-view scroll-y class="contetnt-scroll">
						<view class="scroll-list" v-if="reasonList.length">
							<view class="list-item" :class="{active: selectReason == index}" v-for="(item, index) in reasonList" :key="index" @click="changeReason(index)">
								<view class="item-bg"></view>
								<text class="item-text">{{item}}</text>
							</view>
						</view>
						<view class="scroll-list" v-else>
							<view class="list-item active">
								<view class="item-bg"></view>
								<text class="item-text">其他原因</text>
							</view>
						</view>
					</scroll-view>
				</view>
				<view class="popup-btn">
					<view class="btn" @click="onConfirm">取消订单</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		name: "componentModalCancelOrder",
		data() {
			return {
				// 原因列表
				reasonList: [],
				// 已选原因
				selectReason: 0,
				// 订单信息
				orderInfo: {},
			};
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
			})
		},
		mounted() {
			this.getReasonList()
		},
		methods: {
			// 打开模态框
			open(data) {
				this.orderInfo = JSON.parse(data)
				this.selectReason = 0
				this.$refs.popupModal.open()
			},
			// 获取原因列表
			getReasonList() {
				this.$util.request("passenger.order.cancelReason").then(res => {
					if (res.code == 1) {
						this.reasonList = res.data
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					console.error('获取原因列表 ', error)
				})
			},
			// 选择乘车人
			changeReason(index) {
				this.selectReason = index
			},
			// 跳转乘车人列表
			toPassenger() {
				this.$util.toPage({
					mode: 1,
					path: "/pages/passenger/mine/passenger/index",
					isToken: true,
				})
				this.onClose()
			},
			// 关闭弹窗
			onClose() {
				this.$refs.popupModal.close()
			},
			// 取消订单
			onConfirm() {
				if (this.orderInfo.state == 'accepted' && this.orderInfo.driver_arrive_status == 'not_arrived' && this.orderInfo.isTimeEnd == 2) {
					const endTime = (this.orderInfo.user_cancel_config?.user_free_cancel_end_time || 0) * 1000
					const nowTime = new Date().getTime()
					const timeDiff = endTime - nowTime;
					if (timeDiff <= 0) {
						if (parseInt(this.orderInfo.user_cancel_config?.user_every_day_cancel_count || 0) > 0) {
							uni.showModal({
								content: ` 今天您还有${this.orderInfo.user_cancel_config.user_every_day_cancel_count}次取消机会， \n 是否取消？ `,
								cancelText: "我再想想",
								cancelColor: "#8D929C",
								confirmText: "确认取消",
								confirmColor: this.themeColor,
								success: (res) => {
									if (res.confirm) {
										this.cancelEvent()
									} else {
										this.onClose()
									}
								}
							})
						} else {
							uni.showModal({
								content: ` 取消本次订单，将扣除${this.orderInfo.user_cancel_config?.user_cancel_order_deduct_fees || 0}元给师傅补偿 `,
								cancelText: "我再想想",
								cancelColor: "#8D929C",
								confirmText: "确认取消",
								confirmColor: this.themeColor,
								success: (res) => {
									if (res.confirm) {
										this.cancelEvent()
									} else {
										this.onClose()
									}
								}
							})
						}
						return
					}
				}
				this.cancelEvent()
			},
			// 取消事件
			cancelEvent() {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				this.$util.request("passenger.order.cancel", {
					id: this.orderInfo.id,
					cancel_order_reason: this.reasonList[this.selectReason],
					state: this.orderInfo.state,
					driver_arrive_status: this.orderInfo.driver_arrive_status || "",
				}).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						if (res.data?.situation == 1) {
							uni.showModal({
								content: ` 司机已接单，取消本次订单，将扣除${res.data.user_cancel_config?.user_cancel_order_deduct_fees || 0}元给师傅补偿 `,
								cancelText: "我再想想",
								cancelColor: "#8D929C",
								confirmText: "确认取消",
								confirmColor: this.themeColor,
								success: (res) => {
									if (res.confirm) {
										this.orderInfo.state = "accepted"
										this.orderInfo.driver_arrive_status = "not_arrived"
										this.cancelEvent()
									} else {
										this.onClose()
									}
								}
							})
						} else if (res.data?.situation == 2) {
							uni.showModal({
								content: ` 司机已到达，取消本次订单，将扣除${res.data.user_cancel_config?.user_cancel_order_deduct_fees || 0}元给师傅补偿 `,
								cancelText: "我再想想",
								cancelColor: "#8D929C",
								confirmText: "确认取消",
								confirmColor: this.themeColor,
								success: (res) => {
									if (res.confirm) {
										this.orderInfo.state = "accepted"
										this.orderInfo.driver_arrive_status = "arrived"
										this.cancelEvent()
									} else {
										this.onClose()
									}
								}
							})
						} else if (res.data?.situation == 3) {
							uni.showModal({
								content: ` 司机已到达，取消本次订单，将扣除${res.data.user_cancel_config?.user_cancel_order_deduct_fees || 0}元给师傅补偿 `,
								cancelText: "我再想想",
								cancelColor: "#8D929C",
								confirmText: "确认取消",
								confirmColor: this.themeColor,
								success: (res) => {
									if (res.confirm) {
										this.orderInfo.state = "accepted"
										this.orderInfo.driver_arrive_status = "arrived"
										this.cancelEvent()
									} else {
										this.onClose()
									}
								}
							})
						} else {
							uni.showToast({
								icon: "success",
								title: "取消成功"
							})
							this.onClose()
							this.$emit("onUpdate")
						}
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					uni.hideLoading()
					console.error('取消订单 ', error)
				})
			},
			// 改变事件
			onChange(e) {
				this.$emit("onChange", e.show)
			},
		},
	}
</script>

<style lang="scss">
	.component-modal-cancel-order {
		position: relative;
		z-index: 999;

		.popup-box {
			background: #FFFFFF;
			border-radius: 32rpx 32rpx 0 0;
			padding-top: 32rpx;
			padding-bottom: constant(safe-area-inset-bottom);
			padding-bottom: env(safe-area-inset-bottom);

			.popup-header {
				padding: 0 48rpx;
				position: relative;

				.title {
					color: #242629;
					font-size: 32rpx;
					font-weight: 600;
					line-height: 48rpx;
				}

				.subtitle {
					color: #8D929C;
					font-size: 28rpx;
					font-weight: 600;
					line-height: 40rpx;
					margin-top: 16rpx;
				}

				.close {
					width: 48rpx;
					height: 48rpx;
					position: absolute;
					top: 0;
					right: 48rpx;
				}
			}

			.popup-content {
				padding: 12rpx 32rpx 32rpx;

				.contetnt-scroll {
					overflow: hidden;
					max-height: 60vh;
					min-height: 25vh;

					.scroll-list {
						padding: 0 16rpx;

						.list-item {
							margin-top: 32rpx;
							padding: 30rpx;
							border-radius: 32rpx;
							border: 1px solid #242629;
							position: relative;

							.item-bg {
								position: absolute;
								top: 0;
								left: 0;
								right: 0;
								bottom: 0;
								background-color: var(--theme-color);
								opacity: 0;
							}

							.item-text {
								color: #242629;
								font-size: 32rpx;
								font-weight: 600;
								line-height: 44rpx;
								position: relative;
								z-index: 1;
							}

							&.active {
								border-radius: 32rpx;
								border-color: var(--theme-color);
								background: #F2F8FF;

								.item-bg {
									opacity: .05;
								}

								.item-text {
									color: var(--theme-color);
								}
							}
						}
					}
				}
			}

			.popup-btn {
				padding: 16rpx 48rpx;

				.btn {
					color: #FFFFFF;
					font-size: 28rpx;
					line-height: 40rpx;
					text-align: center;
					padding: 28rpx;
					border-radius: 48rpx;
					background: var(--theme-color);
				}
			}
		}
	}
</style>